#include <future>
#include <iostream>
#include <thread>

void producer(std::promise<int> prom)
{
    std::cout << "Producer: computing value...\n";
    std::this_thread::sleep_for(std::chrono::seconds(2));
    prom.set_value(42); // 设置值到 promise
}

void consumer(std::future<int> fut)
{
    std::cout << "Consumer: waiting for value...\n";
    int value = fut.get(); // 阻塞直到 promise 提供值
    std::cout << "Consumer: got value = " << value << "\n";
}

int main()
{
    std::promise<int> prom;
    std::future<int> fut = prom.get_future();

    std::thread t1(producer, std::move(prom));
    std::thread t2(consumer, std::move(fut));

    t1.join();
    t2.join();

    return 0;
}
